package com.qf.service.impl;

import com.qf.constant.CacheConstants;
import com.qf.constant.OperatorConstants;
import com.qf.feign.CacheFeign;
import com.qf.model.StandardSubmit;
import com.qf.service.FilterChainService;
import com.qf.util.CheckMobileOperator;
import com.qf.util.PushMsgUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Map;

/**
 *
 * 号段补全策略
 * @author 千锋教育
 * @Company http://www.mobiletrain.org/
 * @Version 1.0
 */
@Service("phase")
@Slf4j
public class PhaseFilterServiceImpl implements FilterChainService {

    @Autowired
    CacheFeign cacheFeign;

    @Autowired
    PushMsgUtil pushMsgUtil;


    @Override
    public boolean strategy(StandardSubmit submit) {
        log.info("[策略模块 -- 号段补全策略]  策略开始执行！！ submit = {}", submit);
        //指定运行商 通道和 网关的id
        String mobile = submit.getDestMobile();
        //联通
        if(CheckMobileOperator.isChinaUnicomPhoneNum(mobile)) {
            submit.setOperatorId(OperatorConstants.CHINA_UNICOM);
            submit.setGatewayID(OperatorConstants.CHINA_UNICOM);
        }
        //移动
        else if(CheckMobileOperator.isChinaMobilePhoneNum(mobile)) {
            submit.setOperatorId(OperatorConstants.CHINA_MOBILE);
            submit.setGatewayID(OperatorConstants.CHINA_MOBILE);
        }
        //电信
        else if(CheckMobileOperator.isChinaTelecomPhoneNum(mobile)) {
            submit.setOperatorId(OperatorConstants.CHINA_TELECOM);
            submit.setGatewayID(OperatorConstants.CHINA_TELECOM);
        } else{
            submit.setOperatorId(OperatorConstants.CHINA_UNKNOWN);
            submit.setGatewayID(OperatorConstants.CHINA_UNKNOWN);
        }

        //获取手机号的前  7 位
        String preMobile = mobile.substring(0, 7);

        //根据手机号码的号段，获取 所属的 省 市id
        String instid = cacheFeign.get(CacheConstants.CACHE_PREFIX_PHASE + preMobile);
        //如果省市id 不为空
        if(!StringUtils.isEmpty(instid)) {
            String[] arr = instid.split(",");
            //指定 手机号所属省id
            submit.setProvinceId(Integer.parseInt(arr[0]));
            //手机号 市的id
            submit.setCityId(Integer.parseInt(arr[1]));
        } else {

            //如果省市 id没有获取，说明 手机号  非法
            log.info("[策略模块  -- 号段补全策略]  号段补全测试失败！！");
            //发送日志消息 到 mq中， 后期会 监听队列， 并存储数据到ES
            pushMsgUtil.sendLog(submit, "号段补全策略失败！！");
            //发送报告
            pushMsgUtil.pushReport(submit, "号段补全策略失败!!");

            return false;
        }

        Integer clientID = submit.getClientID();
        Integer gatewayID = submit.getGatewayID();
        //获取运营商的map集合
        Map channlMap = cacheFeign.hMGet(CacheConstants.CACHE_PREFIX_SMS_CHANNEL + submit.getOperatorId()+":"+gatewayID);


        //获取 路由信息
        Map clientMap = cacheFeign.hMGet(CacheConstants.CACHE_PREFIX_ROUTER + clientID + ":" + gatewayID);

        if(!CollectionUtils.isEmpty(channlMap) && !CollectionUtils.isEmpty(clientMap)) {
            // 客户端（千锋教育） 扩展码
            String extendnumber = String.valueOf(clientMap.get("extendnumber"));
            String spnumber = String.valueOf(channlMap.get("spnumber"));

            //发送短信的 源号码
            submit.setSrcNumber(spnumber + extendnumber);


            //设置短信的费用
            String price = String.valueOf(clientMap.get("price"));
            //短信的 费用
            submit.setFee(Long.parseLong(price));
        }

        log.info("[策略模块-- 号段补全策略] 号段补全策略执行成功！！");

        return true;
    }
}
